package com.police.db;

/**
 * Created by LEO on 13-6-6.
 */

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "police.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        //CursorFactory设置为null,使用默认值
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //数据库第一次被创建时onCreate会被调用
    @Override
    public void onCreate(SQLiteDatabase db) {
//        db.execSQL("CREATE TABLE IF NOT EXISTS User" +
//                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, password VARCHAR,phoneNo VARCHAR,isAdmin INTEGER)");
        db.execSQL("CREATE TABLE IF NOT EXISTS User" +
        "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, password VARCHAR,isAdmin INTEGER)");
        db.execSQL("CREATE TABLE IF NOT EXISTS Event" +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, adminName VARCHAR,name VARCHAR, time VARCHAR, userName VARCHAR,phoneNum VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS RelatedPeople" +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, phoneNo VARCHAR, eventName VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS History" +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, userName VARCHAR, time TIMESTAMP, lac VARCHAR,cid VARCHAR,lat VARCHAR,lon VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS SearchRecord" +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, time VARCHAR, lac VARCHAR,cid VARCHAR)");
    }

    //如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("ALTER TABLE User ADD COLUMN other STRING");
        db.execSQL("ALTER TABLE Event ADD COLUMN other STRING");
        db.execSQL("ALTER TABLE RelatedPeople ADD COLUMN other STRING");
        db.execSQL("ALTER TABLE History ADD COLUMN other STRING");
    }
}

